﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Xml.Linq;
using System.Data;


public partial class admin_Member_List : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      Session["url_back_admin"] = Request.RawUrl;
      ShowData();
    }
  }
  UserBLL userData = new UserBLL();
  CommentBLL commentData = new CommentBLL();
  EmailBLL emailData = new EmailBLL();
 
  private void ShowData()
  {
    int mod = Convert.ToInt32(dropMod.SelectedValue);
    if (mod == 0)
      ShowDataMail();
    else if (mod == 1)
      ShowMember();
    else
      ShowAll();
  }

  private void ShowAll()
  {
    var listEmail = emailData.GetAll();
    var listUser = userData.GetAll(true);
    DataTable table = CreateDataSoruce();
    foreach (var item in listUser)
    {
      DataRow row = table.NewRow();
      row["Id"] = item.Id.ToString();
      row["UserName"] = item.UserName;
      row["Name"] = item.Name;
      row["Email"] = item.Email;
      row["Phone"] = item.Phone;
      row["TotalBuy"] = userData.GetTotalBuy(item.Id);
      row["CreateDate"] = item.CreatedDate.ToShortDateString();
      table.Rows.Add(row);
    }

    foreach (var item in listEmail)
    {
      DataRow row = table.NewRow();
      row["Id"] = "";
      row["UserName"] = "";
      row["Name"] = "";
      row["Email"] = item.Email1;
      row["Phone"] = "";
      row["TotalBuy"] = "";
      row["CreateDate"] = item.DateRegister.ToShortDateString();
      table.Rows.Add(row);
    }
    gridView.DataSource = table;
    gridView.DataBind();
  }

  private void ShowMember()
  {
    var list = userData.GetAll(true);
    DataTable table = CreateDataSoruce();
    foreach (var item in list)
    {
      DataRow row = table.NewRow();
      row["Id"] = item.Id.ToString();
      row["UserName"] = item.UserName;
      row["Name"] = item.Name;
      row["Email"] = item.Email;
      row["Phone"] = item.Phone;
      row["TotalBuy"] = userData.GetTotalBuy(item.Id);
      row["CreateDate"] = item.CreatedDate.ToShortDateString();
      table.Rows.Add(row);
    }
    gridView.DataSource = table;
    gridView.DataBind();
  }

  private void ShowDataMail()
  {
    var list = emailData.GetAll();
    DataTable table = CreateDataSoruce();
    foreach (var item in list)
    {
      DataRow row = table.NewRow();
      row["Id"] = "";
      row["UserName"] = "";
      row["Name"] = "";
      row["Email"] = item.Email1;
      row["Phone"] = "";
      row["TotalBuy"] = "";
      row["CreateDate"] = item.DateRegister.ToShortDateString();
      table.Rows.Add(row);
    }
    gridView.DataSource = table;
    gridView.DataBind();
  }

  private DataTable CreateDataSoruce()
  {
    DataTable table = new DataTable();
    table.Columns.Add("Id");
    table.Columns.Add("UserName");
    table.Columns.Add("Name");
    table.Columns.Add("Email");
    table.Columns.Add("Phone");
    table.Columns.Add("TotalBuy");
    table.Columns.Add("CreateDate");
    return table;
  }

 
  private void Enable(bool isEnable)
  {
    foreach (GridViewRow item in gridView.Rows)
    {
      foreach (Control ctrl in item.Cells[0].Controls)
      {
        if (ctrl.GetType() == typeof(CheckBox))
        {
          CheckBox check = (CheckBox)ctrl;
          if (check.Checked)
          {
            int id = Convert.ToInt32(item.Cells[1].Text);
            userData.Enable(id, isEnable);
          }
          break;
        }
      }
    }
    ShowData();
  }
  
 
  protected void btnExport_Click(object sender, EventArgs e)
  {
    Response.AddHeader("content-disposition", "attachment;filename=Danh-sach-thanh-vien.xls");
    Response.Charset = String.Empty;
    Response.ContentType = "application/vnd.xls";

    var sw = new System.IO.StringWriter();
    var hw = new HtmlTextWriter(sw);
    gridView.RenderControl(hw);
    
    Response.Write(sw.ToString());
    Response.End();
  }
  public override void VerifyRenderingInServerForm(Control control)
  {
  }  
  protected void linkLogout_Click(object sender, EventArgs e)
  {
    Session["Admin"] = null;
    Response.Redirect("~/admin/Login.aspx");
  }
  protected void dropParent_SelectedIndexChanged(object sender, EventArgs e)
  {
    lbThongBao.Text = "";
      ShowData();
  }
  protected void btnSendMail_Click(object sender, EventArgs e)
  {
    Response.Redirect("~/admin/Member/SendMail.aspx");
  }
}